package com.hngy.xpq.adapterdemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hngy.xpq.adapterdemo.R;
import com.hngy.xpq.adapterdemo.bean.Data;

import java.util.List;

public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.ViewHolder> {

    private List<Data> listData;

    static class ViewHolder extends RecyclerView.ViewHolder {

        View fileView;
        ImageView imageList;
        TextView nameList;

        public ViewHolder(View itemView) {
            super(itemView);
            fileView = itemView;
            imageList = itemView.findViewById(R.id.imageH);
            nameList = itemView.findViewById(R.id.nameH);
        }
    }

    // 获取到数据
    public HorizontalAdapter(List<Data> list) {
        this.listData = list;
    }

    @NonNull
    @Override
    public HorizontalAdapter.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_h,parent,false);
        final HorizontalAdapter.ViewHolder holder = new HorizontalAdapter.ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull HorizontalAdapter.ViewHolder holder, int position) {
        // 数据绑定
        final Data data = listData.get(position);
        holder.imageList.setImageResource(data.getImage());
        holder.nameList.setText(data.getName());
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

}
